//@ts-ignore 忽略ts校验
import {routerLogin} from "@/router/routerUtils";

export const AUTH_TOKEN_NAME = "AUTH_TOKEN";
export const USERINFO_KEY = "USERINFO";

//设置token到本地缓存里
export const setToken = (token:string)=>{
    localStorage.setItem(AUTH_TOKEN_NAME,token);
}
//从本地缓存里获取token
export const getToken = ()=>{
    let result:string = localStorage.getItem(AUTH_TOKEN_NAME)??"";
    return result;
}
//删除token
export const removeToken = ()=>{
    localStorage.removeItem(AUTH_TOKEN_NAME);
}


//设置用户信息到本地缓存里
export const setUser = (userInfo:any)=>{
    localStorage.setItem('USERINFO',JSON.stringify(userInfo));
}
//从本地缓存里获取用户信息
export const getUser = ()=>{
    let result:string = localStorage.getItem(USERINFO_KEY)??"";
    if (result == null || result == ""){
        console.log("用户信息为空")
        return null;
    }
    return JSON.parse(result)
}
//从本地缓存里获取用户信息，如果没有，那么跳转登录页
export const getMustUser = ()=>{
    let result = getUser();
    if (result == null){
        routerLogin();
    }
    return result;
}
//删除用户信息
export const removeUser = ()=>{
    localStorage.removeItem(USERINFO_KEY);
}

